﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLLPC.Utilities;
using System.IO;
using Advantech.Motion;

namespace VisionCore
{
    public class Status_LoadCV2 : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;
        int RetryTimes = 0;
        int cycCount = 0;

        SystemTimeCounter scStatus = new SystemTimeCounter();

        public void Main()
        {
            bool Existence = Kernel.Exc.DI.X027_CV2_Stop == 1;
            bool Empty = Kernel.Exc.DI.X026_CV2_Exist == 0 && Kernel.Exc.DI.X027_CV2_Stop == 0;


            condition = Kernel.motion.CV2_TrackSW_Y.IsHomeAlready;

            start = condition && AutoMode;
            switch (CV2StatusCode)
            {
                case LDCVStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            if (ProcedureHelper_Exc.CV1_Empty)
                            {
                                CV2StatusCode = LDCVStatus.CheckStatus;
                            }
                        }
                    }
                    break;
                case LDCVStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        CV2StatusCode = LDCVStatus.Empty;
                        if (DryRunMode)
                        {
                            cycCount = 0;
                        }
                    }
                    break;
                case LDCVStatus.Empty:
                    subStatusCode = "10: LoadCV1无料状态";
                    {
                        CV2StatusCode = LDCVStatus.LoadPrepare;
                    }
                    break;
                case LDCVStatus.LoadPrepare:
                    subStatusCode = "11: LoadCV1准备接料";
                    {
                        RetryTimes = 0;
                        if (true)
                        {
                            if (cycCount % 2 == 0)
                            {
                                CV2StatusCode = LDCVStatus.Y_ToLoadPort1;
                            }
                            else
                            {
                                CV2StatusCode = LDCVStatus.Y_ToLoadPort2;
                            }
                        }
                        else
                        {
                            if (Kernel.Exc.DI.X064_UpStr3_SendReady == 1)
                            {
                                CV2StatusCode = LDCVStatus.Y_ToLoadPort1;
                            }
                            else if (Kernel.Exc.DI.X066_UpStr4_SendReady == 1)
                            {
                                CV2StatusCode = LDCVStatus.Y_ToLoadPort2;
                            }
                        }
                    }
                    break;
                #region Port1接料
                case LDCVStatus.Y_ToLoadPort1:
                    subStatusCode = "20: 轨道切换至Port1";
                    {
                        if (ProcedureHelper_Exc.CV2_TrackSW_Y_LDPt1)
                        {
                            scStatus.Start();
                            CV2StatusCode = LDCVStatus.PinCylinder_ToUp_1st;
                        }

                    }
                    break;
                case LDCVStatus.PinCylinder_ToUp_1st:
                    {
                        subStatusCode = "21: 阻挡气缸上升";
                        if (ProcedureHelper_Exc.CV2_Cyl_Up)
                        {
                            CV2StatusCode = LDCVStatus.LoadReq_1st;
                        }
                    }
                    if (scStatus.Duration > 500)
                    {
                        RetryTimes++;
                        if (RetryTimes > 5)
                        {
                            CV2StatusCode = LDCVStatus.Error;
                        }
                    }
                    break;
                case LDCVStatus.LoadReq_1st:
                    subStatusCode = "22: 接料请求";
                    if ((Kernel.Exc.DI.X064_UpStr3_SendReady == 1 && Kernel.Exc.DI.X065_UpStr3_Sending == 1) || true)
                    {
                        CV2StatusCode = LDCVStatus.CV_Run_1st;
                        scStatus.Start();
                    }

                    break;
                case LDCVStatus.CV_Run_1st:
                    subStatusCode = "23: CV运行，Port1接料中";                    
                    if ((Existence) || (DryRunMode && scStatus.Duration > 5000))
                    {
                        CV2StatusCode = LDCVStatus.LoadCompleted_1st;

                    }
                    break;
                case LDCVStatus.LoadCompleted_1st:
                    subStatusCode = "24: 接料完成";
                    {
                        if (ProcedureHelper_Exc.CodeRead2_Pass)
                        {
                            CV2StatusCode = LDCVStatus.CV_Y_ToWork;
                        }
                        else
                        {
                            CV2StatusCode = LDCVStatus.IDRead_1st;
                        }                        
                    }
                    break;

                case LDCVStatus.IDRead_1st:
                    subStatusCode = "25: Port1 读码结果判断";
                    {
                        if (CodeRead2_Completed)
                        {
                            CV2StatusCode = LDCVStatus.CV_Y_ToWork;
                        }
                    }
                    break;
                #endregion
                #region Port2接料
                case LDCVStatus.Y_ToLoadPort2:
                    subStatusCode = "30: 轨道切换至Port2";

                    if (ProcedureHelper_Exc.CV2_TrackSW_Y_LDPt2)
                    {
                        CV2StatusCode = LDCVStatus.PinCylinder_ToUp_2st;
                    }
                    break;
                case LDCVStatus.PinCylinder_ToUp_2st:
                    {
                        subStatusCode = "31: 阻挡气缸上升";
                        if (ProcedureHelper_Exc.CV2_Cyl_Up)
                        {
                            CV2StatusCode = LDCVStatus.LoadReq_2st;
                        }
                    }
                    break;
                case LDCVStatus.LoadReq_2st:
                    subStatusCode = "32: 接料请求";
                    {
                        if ((Kernel.Exc.DI.X066_UpStr4_SendReady == 1 && Kernel.Exc.DI.X067_UpStr4_Sending == 1) || true)
                        {
                            scStatus.Start();
                            CV2StatusCode = LDCVStatus.CV_Run_2st;
                        }
                    }
                    break;
                case LDCVStatus.CV_Run_2st:
                    subStatusCode = "33: CV运行，Port2接料中";
                    if ((Existence) || (DryRunMode && scStatus.Duration > 5000))
                    {
                        CV2StatusCode = LDCVStatus.LoadCompleted_2st;
                    }
                    break;
                case LDCVStatus.LoadCompleted_2st:
                    subStatusCode = "34: 接料完成";
                    {
                        if (ProcedureHelper_Exc.CodeRead2_Pass)
                        {
                            CV2StatusCode = LDCVStatus.CV_Y_ToWork;
                        }
                        else
                        {
                            CV2StatusCode = LDCVStatus.IDRead_2nd;
                        }
                    }
                    break;
                case LDCVStatus.IDRead_2nd:
                    subStatusCode = "35: Port2 读码结果判断";
                    {
                        if (CodeRead2_Completed)
                        {
                            CV2StatusCode = LDCVStatus.CV_Y_ToWork;
                        }
                    }
                    break;
                #endregion
                #region 资料检查

                case LDCVStatus.CV_Y_ToWork:
                    subStatusCode = "40: 定位Y轴到夹持位";
                    {
                        if (ProcedureHelper_Exc.CV2_Y_Work)
                        {
                            CV2StatusCode = LDCVStatus.DataCheck;
                        }
                    }
                    break;
                case LDCVStatus.DataCheck:
                    subStatusCode = "41: 资料检测";
                    {
                        CV2StatusCode = LDCVStatus.CimReport;
                    }
                    break;

                case LDCVStatus.CimReport:
                    subStatusCode = "42: 资料上报";
                    {
                        if (DryRunMode)
                        {
                            if (cycCount % 2 == 0)
                            {
                                CV2StatusCode = LDCVStatus.Y_ToInsp1;
                            }
                            else
                            {
                                CV2StatusCode = LDCVStatus.Y_ToInsp2;
                            }
                        }
                        else
                        {
                            if (Insp3StatusCode == InspStatus.Load)
                            {
                                CV2StatusCode = LDCVStatus.Y_ToInsp1;
                            }
                            else if (Insp4StatusCode == InspStatus.Load)
                            {
                                CV2StatusCode = LDCVStatus.Y_ToInsp2;
                            }
                        }
                    }
                    break;
                #endregion
                #region 向模组1出料
                case LDCVStatus.Y_ToInsp1:
                    subStatusCode = "50: 轨道切换至检测模组1";
                    {
                        if (ProcedureHelper_Exc.CV2_TrackSW_Y_Insp1)
                        {
                            CV2StatusCode = LDCVStatus.PinCylinder_ToDown_1st;
                        }
                    }
                    break;
                case LDCVStatus.PinCylinder_ToDown_1st:
                    {
                        subStatusCode = "51: 阻挡气缸下降";
                        if (CV2_Cyl_Dn)
                        {
                            CV2StatusCode = LDCVStatus.Send_Wait_1st;
                        }
                    }
                    break;
                case LDCVStatus.Send_Wait_1st:
                    subStatusCode = "52: 发送准备";
                    if (Insp3StatusCode == InspStatus.Receiving)
                    {
                        CV2StatusCode = LDCVStatus.SendOut_Insp1;
                    }
                    break;
                case LDCVStatus.SendOut_Insp1:
                    subStatusCode = "53: 向模组1出料中";
                    if (Insp3StatusCode == InspStatus.RevCompleted)
                    {
                        CV2StatusCode = LDCVStatus.SendCompleted_1st;
                    }
                    break;
                case LDCVStatus.SendCompleted_1st:
                    subStatusCode = "54: 向模组1出料结束";
                    {
                        if (Empty)
                        {
                            CV2StatusCode = LDCVStatus.End;
                        }
                    }
                    break;
                #endregion
                #region  向模组2出料
                case LDCVStatus.Y_ToInsp2:
                    subStatusCode = "60: 轨道切换至检测模组2";
                    {
                        if (ProcedureHelper_Exc.CV2_TrackSW_Y_Insp2)
                        {
                            CV2StatusCode = LDCVStatus.PinCylinder_ToDown_2st;
                        }
                    }
                    break;
                case LDCVStatus.PinCylinder_ToDown_2st:
                    {
                        subStatusCode = "61: 阻挡气缸下降";
                        if (CV2_Cyl_Dn)
                        {
                            CV2StatusCode = LDCVStatus.Send_Wait_2st;
                        }
                    }
                    break;
                case LDCVStatus.Send_Wait_2st:
                    subStatusCode = "62: 发送准备";
                    if (Insp4StatusCode == InspStatus.Receiving)
                    {
                        CV2StatusCode = LDCVStatus.SendOut_Insp2;
                    }
                    break;
                case LDCVStatus.SendOut_Insp2:
                    subStatusCode = "63: 向模组2出料中";
                    if (Insp4StatusCode == InspStatus.RevCompleted)
                    {
                        CV2StatusCode = LDCVStatus.SendCompleted_2st;

                    }
                    break;
                case LDCVStatus.SendCompleted_2st:
                    subStatusCode = "64: 向模组2出料结束";
                    {
                        if (Empty)
                        {
                            CV2StatusCode = LDCVStatus.End;
                        }
                    }
                    break;
                #endregion

                case LDCVStatus.End:
                    subStatusCode = "100: 流程结束";
                    {
                        if (DryRunMode)
                        {
                            cycCount++;
                        }
                        CV2StatusCode = LDCVStatus.LoadPrepare;
                    }
                    break;
                case LDCVStatus.Error:
                    subStatusCode = "90: 报警停止";
                    {
                    }
                    break;
                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("入料CV2-" + tempStatusCode);
            }
        }




    }
}
